package com.mybatis.model;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.inter.IUserOperation;

public class UserDB {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;

    static{
        try{
            reader    = Resources.getResourceAsReader("Configuration.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSessionFactory getSession(){
        return sqlSessionFactory;
    }

    public void getUserList(String userName){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            List<User> users = userOperation.selectUsers(userName);
            for(User user:users){
                System.out.println("id:"+user.getId()+"name:"+user.getName()
                        +"sex:"+user.getSex()+"login:"+user.getLogin()+"password:"+user.getPassword()
                        +"phone:"+user.getPhone()+"department:"+user.getDepartment()
                        +"position:"+user.getPosition()+"role:"+user.getRole());
            }

        } finally {
            session.close();
        }
    }

    //获取密码
    public String getPassword(String user_login) {
        SqlSession session = sqlSessionFactory.openSession();
        String password = null;
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            password = userOperation.selectPasswordByLogin(user_login);
            //System.out.println(password);
        } finally {
            session.close();
        }

        return password;
    }

    //获取用户
    public User getUser(String user_login) {
        SqlSession session = sqlSessionFactory.openSession();
        User user;
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            user = userOperation.selectUserByLogin(user_login);
            //System.out.println(user.getPassword());
        } finally {
            session.close();
        }

        return user;
    }
    public void addUser(String name,String sex,String login,String password,String phone,String department,String position,String role){
        User user=new User();
        user.setName(name);
        user.setSex(sex);
        user.setLogin(login);
        user.setPassword(password);
        user.setPhone(phone);
        user.setDepartment(department);
        user.setPosition(position);
        user.setRole(role);
        SqlSession session = sqlSessionFactory.openSession();
        try {
            IUserOperation userOperation=session.getMapper(IUserOperation.class);
            userOperation.addUser(user);
            session.commit();
            System.out.println("当前增加的用户 id为:"+user.getId());
        } finally {
            session.close();
        }
    }
//    public static void main(String[] args) {
//        UserDB udb=new UserDB();
//        udb.getPassword("x0x0x01");
//    }
}
